package com.highstockings.controller;

import com.highstockings.model.User;
import com.highstockings.service.ArticleService;
import com.highstockings.service.GalleryTypeService;
import com.highstockings.service.ModelService;
import com.highstockings.service.UserService;
import com.highstockings.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * 后台业务逻辑
 * * Created by diablo13064 on 2018/3/20.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    //private static String dir = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"/static/upload/";
    @Autowired
    private ModelService modelService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private GalleryTypeService galleryTypeService;
    @Autowired
    private UserService userService;
    /**
     * 登陆页面
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "admin/partner/login";
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "admin/login";
    }

    @RequestMapping("/entry")
    public String entry(User user, HttpSession session) {
        User currentUser = userService.isExist(user);
        if (currentUser == null) {
            return "admin/partner/login";
        }
        session.setAttribute("currentUser", currentUser);
        return "admin/partner/website_statistics";
    }


    /**
     * 后台主页（分析）
     * @return
     */
    @RequestMapping("/statistics")
    public String statistics(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "后台统计");
        return "admin/partner/website_statistics";
    }

    /**
     * 管理内容
     * @return
     */
    @RequestMapping("/mcontent")
    public String mcontent(ModelMap modelMap,
                           @RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "7")int pageSize) {
        modelMap.addAttribute("pageName", "内容管理");
        modelMap.addAttribute("articleList", articleService.findAllArticleDetails());
        return "admin/partner/manage_content";
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping("/econtent")
    public String econtent(ModelMap modelMap,HttpSession session) {
        modelMap.addAttribute("pageName", "文章编辑");
        //modelMap.addAttribute("modelNames",modelService.findAllModel().size()>0?modelService.findAllModel():null);
        //modelMap.addAttribute("articleTypes",galleryTypeService.findAllGalleryType().size()>0?galleryTypeService.findAllGalleryType():null);
       /* User user = userService.selectUserById(1);
        session.setAttribute("sessionUser",user);
        modelMap.addAttribute("currentUser",user);*/

        FileUtil.DeleteAllFile(new File(articleService.getUploadDir()), ".jpg");
        return "admin/partner/edit_content";
    }

    /**
     * 日志
     * @param modelMap
     * @return
     */
    @RequestMapping("/log")
    public String log(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "站点日志");
        return "admin/partner/log_counting";
    }

    /**
     * 留言管理
     * @return
     */
    @RequestMapping("/leavemessage")
    public String leavemesage(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "留言管理");
        return "admin/partner/leaving_message";
    }

    /**
     * 账户管理
     * @return
     */
    @RequestMapping("/maccounting")
    public String maccounting(ModelMap modelMap) {
        FileUtil.DeleteAllFile(new File(articleService.getUploadDir()), ".jpg");
        modelMap.addAttribute("pageName", "账号管理");
        return "admin/partner/manage_accounting";
    }

    /**
     * 账户管理
     * @return
     */
    @RequestMapping("/mmodel")
    public String mmodel(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "模特管理");
        modelMap.addAttribute("modelList", modelService.findAllModel());
        return "admin/partner/manage_model";
    }
    /**
     * 扩展、推广
     * @return
     */
    @RequestMapping("/webextend")
    public String webextend(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "网站推广");
        return "admin/partner/website_extend";
    }

    /**
     * 友链
     * @return
     */
    @RequestMapping("/mfriend")
    public String mfriend(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "友链管理");
        return "admin/partner/manage_friendlinks";
    }

    @RequestMapping("/oops")
    public String errorpage(ModelMap modelMap) {
        return "admin/partner/404";
    }
    @RequestMapping("/about")
    public String about(ModelMap modelMap) {
        modelMap.addAttribute("pageName", "关于&其他");
        return "admin/partner/about_others";
    }


}
